package com.atguigu.threadtest;

/**
 * ClassName: Phone
 * Package: com.atguigu
 * Description:
 *
 * @author: fu-wang
 * @create: 2024/10/28 18:21
 * version: 1.0
 *
 *
 * 1. 先访问短信，再访问邮件，先打印短信还是邮件
 * ====================
 * thread Sms,sendSms
 * thread Email,sendEmail
 * ==================== 短信优先
 *
 * 2. 停4秒在短信方法内，先打印短信还是邮件
 * ====================
 * thread Sms,sendSms
 * thread Email,sendEmail
 * ==================== 短信先天优势，且Thread.sleep睡眠时不释放线程
 *
 * 3. 先访问短信，再访问hello方法，是先打短信还是hello
 * ====================
 * thread hello,hello
 * thread Sms,sendSms
 * ==================== hello不抢占锁，优先打印
 *
 * 4. 现在有两部手机，第一部发短信，第二部发邮件，先打印短信还是邮件
 * ====================
 * thread Sms,sendSms
 * thread Email,sendEmail
 * ==================== 互相不抢占对方的锁，打印顺序随机
 *
 * 5. 两个静态同步方法，1部手机，先打印短信还是邮件
 * ====================
 * thread Email,sendEmail
 * thread Sms,sendSms
 * ==================== 随机
 *
 * 6. 两个静态同步方法，2部手机，先打印短信还是邮件
 *====================
 * thread Sms,sendSms
 * thread Email,sendEmail
 * ==================== 随机，互相不抢占锁
 *
 * 7. 1个静态同步方法，1个普通同步方法，1部手机，先打印短信还是邮件
 *====================
 * thread Email,sendEmail
 * thread Sms,sendSms
 * ==================== 随机
 *
 * 8. 1个静态同步方法，1个普通同步方法，2部手机，先打印短信还是邮件
 *====================
 * thread Email,sendEmail
 * thread Sms,sendSms
 * ==================== 随机
 */
public class Phone {
    public synchronized static void sendSms() throws InterruptedException {
//        Thread.sleep(4000);
        System.out.println(Thread.currentThread().getName()+",sendSms");
    }
    public synchronized void sendEmail(){
        System.out.println(Thread.currentThread().getName()+",sendEmail");
    }
    public void hello(){
        System.out.println(Thread.currentThread().getName()+",hello");
    }
}

class ThreadTest{
    public static void main(String[] args) {
        Phone phone = new Phone();
        Phone phone1 = new Phone();
        System.out.println("====================");
        new Thread(()->{
            try {
                phone.sendSms();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"thread Sms").start();

        new Thread(()->{
//            phone.sendEmail();
            phone1.sendEmail();
                    },"thread Email").start();

//        new Thread(()->{
//            phone.hello();
//                    },"thread hello").start();






        //分割线
        new Thread(()->{
            try {
                Thread.sleep(4100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("====================");}).start();

    }
}
